//*************BEGIN FUNCTION TO MOVE ITEMS FROM ONE SELECT TO ANOTHER*********************

function includeItem(formname, includeBox, removeBox, hiddenName)
{
	var selected = document.forms[formname][includeBox].selectedIndex;
	if (document.forms[formname][includeBox].value != "") {
		var toAddText = document.forms[formname][includeBox][selected].text;
		var toAddValue = document.forms[formname][includeBox].value;
		var newOpt = new Option(toAddText, toAddValue);
		var lengthList = document.forms[formname][removeBox].length;
		document.forms[formname][removeBox][lengthList] = newOpt;
		document.forms[formname][includeBox].remove(selected);	
	}

}
function removeItem(formname, removeBox, includeBox, hiddenName) {
	var selected = document.forms[formname][removeBox].selectedIndex;
	if (document.forms[formname][removeBox].value != "") {	
		var toAddText = document.forms[formname][removeBox][selected].text;	
		var toAddValue = document.forms[formname][removeBox].value;
		var newOpt = new Option(toAddText, toAddValue);
		var lengthList = document.forms[formname][includeBox].length;
		document.forms[formname][includeBox][lengthList] = newOpt;
		document.forms[formname][removeBox].remove(selected);			
	}
}

//*************END OF FUNCTION TO MOVE ITEMS FROM ONE SELECT TO ANOTHER********************		
// async delete image flow
//1. request is fired to cmsBinaryDataDelete.php
/** 
	fires an async action to perform the image deletion 
*/
function goDeleteBinaryDataInDocumentForElement( documentcode, elementid) {
	if (confirm('Are you sure you want to delete the selected file')){
//		alert('yes');//
		var opts = {
				method: 'post', 
				parameters: 'documentCode=' + documentcode + '&elementId=' +elementid, 
				asynchronous: true, 
				evalScripts:true,
				onSuccess: deleteBinaryDataSucceeded, 
				onFailure: deleteBinaryDataFailed
		};
		var ajaxRequest = new Ajax.Request('/cms/html/lib/cmsBinaryDataDelete.php', opts);

	} else {
//		alert('no');	//
//		do nothing		//
	}
	return false;
}


function deleteBinaryDataSucceeded(response){
	// remove the delete button
	// remove the picutre details.
	// do mojo node yanking.... 
	// alert('Data Deleted');
	removeNodeWithId('cms_fom_filedetails_' + response.responseText);
	removeNodeWithId('cms_fom_deletefile_' + response.responseText);
}

function removeNodeWithId( id ) {
	var node = document.getElementById(id);
	if (null != node){
		node.parentNode.removeChild( node );	
	}
}

function deleteBinaryDataFailed(response){
	alert( "Failed to Delete Data, sorry it didnt work out.");
}

	function imageSubmit(input) {
		f = input.form;
		n = document.createElement('input');
		n.setAttribute('type','hidden');
		n.name = input.name;
		n.value = input.value;
		f.appendChild(n);
		return true;
	}
	
	function jsDualStateToggle(checkbox,hiddenid,onvalue,offvalue){
		h = document.getElementById(hiddenid);
		if(checkbox.checked) {
			h.value = onvalue;
		} else {
			h.value = offvalue;
		}
	}
	
	
//Start functions for Dual Checkbox List stuff :-)

function initChecklist() {
    if (document.all && document.getElementById) {
        // Get all unordered lists
	   var lists = document.getElementsByTagName("ul");
        
        for (i = 0; i < lists.length; i++) {
            var theList = lists[i];
            
		  // Only work with those having the class "checklist"
            if (theList.className.indexOf("checkList") > -1) {
                var labels = theList.getElementsByTagName("label");
                
			 // Assign event handlers to labels within
                for (var j = 0; j < labels.length; j++) {
                    var theLabel = labels[j];
                    theLabel.onmouseover = function() { this.className += " hover"; };
                    theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
                }
            }
        }
    }
}

		function findAncestorOfType(node, type){
			if (null == node) 
			 	return null;

			if (null == type || 0 >= type.length  )
				return null;
				
			return findAncestorOfTypeR(node, type.toUpperCase());
		}
		
		function findAncestorOfTypeR(node, type){
			var parent = node.parentNode;
			
			if (null == parent)
				return null;
				
			if( type == parent.tagName.toUpperCase() ){
				return parent;
			} else {
				return findAncestorOfTypeR(parent, type );
			}
		}
		
		/*
		function sortCheckBoxList(listId) {
			//we take the ul.. and 
			// try and sort the li's the problem, however is getting the text inside the node.. 
			var list = document.getElementById(listId);
			quickSortList(list,0,list.childNodes.length -1 );
		}*/
		
		// 0 == same
		// -1 less than
		// 1 greater than
		function compareListNodes(a,b) {
				
			// two non li items are equal	
			if (a.tagName !="LI" && b.tagName !="LI") 
				return 0;

			//the first as an li is less than
			if (a.tagName !="LI")
				return -1;
			
			// an li.. is always greater than a non li	
			if (b.tagName !="LI")
				return 1;
							
			//otherwise compare the strings.
			//this could be slow.. not sure			
			var sA = ( a.childNodes[0].lastChild.nodeValue );  
			var sB = ( b.childNodes[0].lastChild.nodeValue );  
			
			if (sA == sB)
				return 0;
			else if (sA < sB)
				return -1;
			else
				return 1;
				
		}
				
		function selectCheckBoxListItem(node){

			if (null == node || null == node.id)
				node = this;
								
			parentLI = findAncestorOfType(node, "LI");
			if (null == parentLI)
				return;
			
			parentUL = parentLI.parentNode;
			parentUL.removeChild( parentLI );
						
			node.onclick = deselectCheckBoxListItem;
			
			var selectedId = parentUL.id.replace(/unselected/,"selected");
			var selectedUL = document.getElementById( selectedId );
			
			insertNodeInPosition(selectedUL, parentLI);
			
			node.checked = true;
			
			return true;
		}
		
		function deselectCheckBoxListItem(node){

			if (null == node || null == node.id)
				node = this;
			
			parentLI = findAncestorOfType(node, "LI");
			
			parentUL = parentLI.parentNode;
			parentUL.removeChild( parentLI );
						
			node.onclick = selectCheckBoxListItem;

			var unselectedId = parentUL.id.replace(/selected/,"unselected");
			var unselectedUL = document.getElementById( unselectedId );
			

			//unselectedUL.appendChild(parentLI);
			insertNodeInPosition( unselectedUL, parentLI);
			node.checked = false;
				
			return true;
		}
	
		function insertNodeInPosition(parentList, nodeToInsert){
			
			//empty case
			if (0 == parentList.childNodes.length) {
				parentList.appendChild(nodeToInsert);
			}
			var max = parentList.childNodes.length -1;
			var min = 0;
			var inserted = false;
			while ( min <= max ){
				var mid = Math.floor((max  + min) / 2);
				
				//var midNode = parentList.childNodes[mid];
				var i = 0;
				while ( 0 < (mid - i) && parentList.childNodes[mid - i].tagName != "LI"){
					i++;
				}
				var midNode = parentList.childNodes[mid - i ];
				if (null == midNode)
					midNode = parentList.firstChild;
					
				// we need to check and see if we are less than or equalTo
				var midCmp = compareListNodes(nodeToInsert, midNode );
				if (0 == midCmp ) {// we are equal.. so we can just insert before mid..
					parentList.insertBefore( nodeToInsert, midNode);
					return;
				} else {
					if ( 0 > midCmp){
						// we are less than.. we will look one node ahead.. and see if we are less than that
						// if we are, we can insert, otherwise fiddle the bounds and loop.
						
						var i = 1;
						while ( 0 < (mid - i) && parentList.childNodes[mid - i].tagName != "LI"){
							i++;
						}

						var nextSibling = parentList.childNodes[mid - i];
						if (null == nextSibling){
							parentList.insertBefore( nodeToInsert, parentList.childNodes[0] );
							return;
						}
												
						var downCmp = compareListNodes(nodeToInsert, nextSibling);
						if (0 <= downCmp ) {
							// we are greater then the one below mid.. so we have an insert point
							// before the mid..
							parentList.insertBefore(nodeToInsert, midNode );
							return;
						} else {
							// we are still less.. so we can move mid to be max
							max = (mid - i);	
						}
						
					} else if (0 < midCmp){
						// we are greater than, we will look one node back.. and see if we are greater than than
						// if we are we can insert.. otherwise fiddle the bounds and loop

						
						var i = 1;
						while ((parentList.childNodes.length -1 ) > (mid+i) && parentList.childNodes[mid + i].tagName != "LI"  ){
							i++;
						}

						var nextSibling = parentList.childNodes[mid + i];
						if (null == nextSibling){
							parentList.appendChild(nodeToInsert);
							return;
						}
						var upCmp = compareListNodes(nodeToInsert, nextSibling);
						if (0 >= upCmp) {
							parentList.insertBefore(nodeToInsert, nextSibling);
							return;
						} else {
							// we are still bigger than mid.. 
							min = (mid + i);
						}
					}
					
				}
			}
			alert('did we actually do an insertion if we get here?');
			return;
		}

//End functions for Dual Checkbox List stuff :-)